﻿using Common;
using Common.Helper;
using Extensions;
using IService.Blog;
using log4net;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace BlogAPI.Controllers
{
    /// <summary>
    /// 主页管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    //[AllowAnonymous]
    public class HomeController : ControllerBase
    {
        readonly ILogger<HomeController> logger;
        readonly IHomeService homeService;
        readonly SignalRHub signalRHub;
        public HomeController(ILogger<HomeController> _logger, IHomeService _homeService, SignalRHub _signalRHub)
        {
            logger = _logger;
            homeService = _homeService;
            signalRHub = _signalRHub;
        }

        /// <summary>
        /// 获取计算题验证码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<MessageModel<NumCode>> GetNumCode()
        {
            try
            {
                return await homeService.GetNumCode(); 
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                LogManager.GetLogger(typeof(HomeController)).Debug(ex.Message);
                return new MessageModel<NumCode>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }
        }

        /// <summary>
        /// 获取图片验证码路径
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("获取图片验证码路径")]
        [AllowAnonymous]
        public MessageModel<string> GetVCUrl()
        {
            try
            {
                string result = HttpContext.Request.GetDisplayUrl();
                return new MessageModel<string>
                {
                    Success = true,
                    Code = 200,
                    Data = result.Substring(0, result.LastIndexOf('/')+1)+ "GetVerificationCode"
                };
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                LogManager.GetLogger(typeof(HomeController)).Debug(ex.Message);
                return new MessageModel<string>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }
        }

        /// <summary>
        /// 获取图片验证码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Description("获取图片验证码")]
        [AllowAnonymous]
        public async Task<IActionResult> GetVerificationCode(string id)
        {
            try
            {
                AppSettings.write("VCID",id);//存储验证码ID
                MessageModel<VerifyCode> model = await homeService.GetVerificationCode(id);
                return File(model.Data.Image, "image/png");
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                //return new MessageModel<IActionResult>()
                //{
                //    Message = "服务异常" + ex.Message,
                //};
                return null;
            }
        }

        /// <summary>
        /// 获取验证码
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("获取验证码")]
        [AllowAnonymous]
        public async Task<MessageModel<string>> GetVerCode(string id)
        {
            try
            {
                AppSettings.write("VCID", id);//存储验证码ID
                MessageModel<VerifyCode> model = await homeService.GetVerificationCode(id);
                return new MessageModel<string> { 
                    Data= model.Data.Code,
                    Code = 200,
                    Message = "获取成功",
                    Success = true
                };
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                //return new MessageModel<IActionResult>()
                //{
                //    Message = "服务异常" + ex.Message,
                //};
                return null;
            }
        }

        /// <summary>
        /// 判断验证码是否正确
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Description("判断验证码是否正确")]
        [AllowAnonymous]
        public async Task<MessageModel<bool>> Validate(string id, string code)
        {
            try
            {
                return await homeService.Validate(id,code);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<bool>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }
           
        }

        /// <summary>
        /// 博文统计图
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("博文统计图")]
        public async Task<MessageModel<string>> GetStatistics()
        {
            try
            {
                return await homeService.GetStatistics();
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<string>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }
        }

        /// <summary>
        /// 查询数据库表名及数据量
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("查询数据库表名及数据量")]
        public async Task<MessageModel<string>> GetAllBase()
        {
            try
            {
                return await homeService.GetAllBase();
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<string>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }
        }

        /// <summary>
        /// 获取在线的人
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("获取在线的人")]
        [AllowAnonymous]
        public MessageModel<List<User>> GetOnline()
        {
            List<User> users = signalRHub.GetOnline();
            return new MessageModel<List<User>>()
            {
                Data = users,
                Code = 200,
                Success = true,
                Message = ""
            };
        }

    }
}
